package com.zelin.reference;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * TCP协议
 * 服务端
 */
public class TcpServer {
    public static void main(String[] args) {
        ServerSocket server = null;
        Socket scocket = null;
        try {
            //创建服务端 端口 -每个程序在一台计算机上的唯一标识
            server = new ServerSocket(6666);
            System.out.println("服务端创建....");

            while(true){
                //等待客户端连接，直到有客户端接入，返回接入的客户端对象
                scocket = server.accept();
                System.out.println("有客户端接入！");

                //创建线程，开启多线程运行登录的过程
                LoginThread loginThread = new LoginThread(scocket);
                loginThread.start();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert scocket != null;
                scocket.close();
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
